package com.fubowen.reader.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.fubowen.reader.R
import com.fubowen.reader.adapter.SimpleListAdapter
import com.fubowen.reader.databinding.PublicTreeFilterBinding
import com.fubowen.reader.db.bean.WebSiteConfigBean
import com.fubowen.reader.extension.isRegex
import com.fubowen.reader.extension.setEmpty
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

const val LINK_NAME = "LINK_NAME"
const val LINK_PATH = "LINK_PATH"

class TreeFilterFragment : ThemeFragment() {

    private lateinit var adapter: SimpleListAdapter<WebSiteConfigBean>
    private lateinit var binding: PublicTreeFilterBinding

    override var isDialog = true

    //region onCreateView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = PublicTreeFilterBinding.inflate(inflater)
        initPredefineFilter()
        binding.submit.setOnClickListener { submit() }
        binding.reset.setOnClickListener { reset() }
        return binding.root
    }

    //endregion

    private fun initPredefineFilter() {
        val layoutManager = LinearLayoutManager(requireContext())
        adapter = SimpleListAdapter(R.layout.public_tree_filter_item, requireContext()) { view ->
            view.setOnClickListener {
                val position = layoutManager.getPosition(it)
                val item = adapter.currentList[position]
                when (item.key) {
                    LINK_NAME -> binding.nameValue.setText(item.value)
                    LINK_PATH -> binding.linkValue.setText(item.value)
                }
            }
        }
        binding.predefineFilter.layoutManager = layoutManager
        binding.predefineFilter.adapter = adapter
        val predefine = mutableListOf(
            WebSiteConfigBean(key = LINK_NAME, label = "链接名称", value = "^$", description = "筛选出未命名链接"),
            WebSiteConfigBean(key = LINK_NAME, label = "链接名称", value = ".*《.+?》.*", description = "筛选出包含书名号的链接"),
            WebSiteConfigBean(key = LINK_NAME, label = "链接名称", value = ".*全集.*", description = "筛选出包含全集两个字的链接"),
            WebSiteConfigBean(key = LINK_PATH, label = "链接地址", value = ".*\\d+(\\.html)?\$", description = "筛选出数字.html结尾的链接"),
        )
        lifecycleScope.launch {
            val data = withContext(Dispatchers.IO) { database.webSiteDao().getSpiderByKeyList(listOf(LINK_NAME, LINK_PATH)) }
            if (data.isNotEmpty()) {
                predefine.addAll(data)
            }
            adapter.submitList(predefine)
        }
    }

    //region submit
    private fun submit() {
        val data = mutableListOf<WebSiteConfigBean>()
        val nameValue = binding.nameValue.text.toString()
        if (nameValue.isNotBlank() && nameValue.isRegex()) {
            data.add(WebSiteConfigBean(key = LINK_NAME, label = "链接名称", value = nameValue))
        }
        val linkValue = binding.linkValue.text.toString()
        if (linkValue.isNotBlank() && linkValue.isRegex()) {
            data.add(WebSiteConfigBean(key = LINK_PATH, label = "链接地址", value = linkValue))
        }
        cacheViewModel.filterList(data)
        dismiss()
    }
    //endregion

    //region reset
    private fun reset() {
        val data = mutableListOf<WebSiteConfigBean>()
        binding.nameValue.setEmpty()
        binding.linkValue.setEmpty()
        cacheViewModel.filterList(data)
        dismiss()
    }
    //endregion
}